 package sg.edu.nus.iss.billing.vo.customer;

import java.text.SimpleDateFormat;
import java.util.Date;

import sg.edu.nus.iss.billing.common.DbUtil;
import sg.edu.nus.iss.billing.common.exception.InvalidParamsException;
import sg.edu.nus.iss.billing.vo.account.AccountVO;
import sg.edu.nus.iss.billing.vo.common.IAuditCreate;
import sg.edu.nus.iss.billing.vo.common.IAuditUpdate;
import sg.edu.nus.iss.billing.vo.common.IDbSerialisable;

public class CustomerVO implements IAuditCreate, IAuditUpdate, IDbSerialisable{

	private final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss a");
	
	private String customerId;
	private String sltn;
	private String customerNme;
	private String emailTxt;
	private String nric;
	private AddressVO  address;
	private String contactNo;
	private String interest;
	private boolean status;
	private String createUseridNum;
	private String updateUseridNum;
    private Date createTme;
    private Date updateTme;
    private String trmntUseridNum;
    private Date trmntTme;
    private AccountVO account = new AccountVO();
    
	/**
	 * @return the account
	 */
	public AccountVO getAccount() {
		return account;
	}
	/**
	 * @param account the account to set
	 */
	public void setAccount(AccountVO account) {
		this.account = account;
	}
	/**
	 * @return the customerId
	 */
	public String getCustomerId() {
		return customerId;
	}
	/**
	 * @param customerId the customerId to set
	 */
	public void setCustomerId(String customerId) {
		this.customerId = customerId;
	}
	/**
	 * @return the customerNme
	 */
	public String getCustomerNme() {
		return customerNme;
	}
	/**
	 * @param customerNme the customerNme to set
	 */
	public void setCustomerNme(String customerNme) {
		this.customerNme = customerNme;
	}
	
	/**
	 * @return the sltn
	 */
	public String getSltn() {
		return sltn;
	}
	/**
	 * @param sltn the sltn to set
	 */
	public void setSltn(String sltn) {
		this.sltn = sltn;
	}
	
	/**
	 * @return the emailTxt
	 */
	public String getEmailTxt() {
		return emailTxt;
	}
	/**
	 * @param emailTxt the emailTxt to set
	 */
	public void setEmailTxt(String emailTxt) {
		this.emailTxt = emailTxt;
	}
	/**
	 * @return the nric
	 */
	public String getNric() {
		return nric;
	}
	/**
	 * @param nric the nric to set
	 */
	public void setNric(String nric) {
		this.nric = nric;
	}
	/**
	 * @return the address
	 */
	public AddressVO getAddress() {
		return address;
	}
	/**
	 * @param address the address to set
	 */
	public void setAddress(AddressVO address) {
		this.address = address;
	}
	/**
	 * @return the contactNo
	 */
	public String getContactNo() {
		return contactNo;
	}
	/**
	 * @param contactNo the contactNo to set
	 */
	public void setContactNo(String contactNo) {
		this.contactNo = contactNo;
	}
	/**
	 * @return the interest
	 */
	public String getInterest() {
		return interest;
	}
	/**
	 * @param interest the interest to set
	 */
	public void setInterest(String interest) {
		this.interest = interest;
	}
	/**
	 * @return the status
	 */
	public boolean isStatus() {
		return status;
	}
	/**
	 * @param status the status to set
	 */
	public void setStatus(boolean status) {
		this.status = status;
	}
	/**
	 * @return the createUseridNum
	 */
	public String getCreateUseridNum() {
		return createUseridNum;
	}
	/**
	 * @param createUseridNum the createUseridNum to set
	 */
	public void setCreateUseridNum(String createUseridNum) {
		this.createUseridNum = createUseridNum;
	}
	/**
	 * @return the createTme
	 */
	public Date getCreateTme() {
		return createTme;
	}
	/**
	 * @param createTme the createTme to set
	 */
	public void setCreateTme(Date createTme) {
		this.createTme = createTme;
	}
	/**
	 * @return the updateTme
	 */
	public Date getUpdateTme() {
		return updateTme;
	}
	/**
	 * @param updateTme the updateTme to set
	 */
	public void setUpdateTme(Date updateTme) {
		this.updateTme = updateTme;
	}
	/**
	 * @return the trmntUseridNum
	 */
	public String getTrmntUseridNum() {
		return trmntUseridNum;
	}
	/**
	 * @param trmntUseridNum the trmntUseridNum to set
	 */
	public void setTrmntUseridNum(String trmntUseridNum) {
		this.trmntUseridNum = trmntUseridNum;
	}
	/**
	 * @return the trmntTme
	 */
	public Date getTrmntTme() {
		return trmntTme;
	}
	/**
	 * @param trmntTme the trmntTme to set
	 */
	public void setTrmntTme(Date trmntTme) {
		this.trmntTme = trmntTme;
	}
	/**
	 * @return the updateUseridNum
	 */
	public String getUpdateUseridNum() {
		return updateUseridNum;
	}
	/**
	 * @param updateUseridNum the updateUseridNum to set
	 */
	public void setUpdateUseridNum(String updateUseridNum) {
		this.updateUseridNum = updateUseridNum;
	}
	/*
	 * Convert trmtTme(Date) to String
	 */
	public  String getTrmtTmeString(){
		try{
			return sdf.format(trmntTme);
		}catch(Exception e){
			return "";
		}
		
	}
	
	/*
	 * Convert createTme(Date) to String
	 */
	public String getCreateTmeString(){
		try{
			return sdf.format(createTme);
		}catch(Exception e){
			return "";
		}
	}
	
	/*
	 * Convert updateTme(Date) to String
	 */
	public String getUpdateTmeString(){
		try{
			return sdf.format(updateTme);
		}catch(Exception e){
			return "-";
		}
	}
	@Override
	public void setParameters(String serialisedEntry)
			throws InvalidParamsException {
		String[] tokens =  serialisedEntry.split(java.util.regex.Pattern.quote(DbUtil.DELIMITER));
		if (tokens.length != 5) {
			throw new InvalidParamsException("Expected 5 parameters, received " + tokens.length);
		} else {
			getAccount().setAccountNo(tokens[0]); 
			setNric(tokens[1]);
			setCustomerNme(tokens[2]);
			setContactNo(tokens[3]);
			setInterest(tokens[4]);
		}
		
	}
	@Override
	public String serialised() {
		// accountNo^nric^name^assignNo^interest
		StringBuilder sb = new StringBuilder();
		sb.append(getAccount().getAccountNo());
		sb.append(DbUtil.DELIMITER);
		sb.append(getNric());
		sb.append(DbUtil.DELIMITER);
		sb.append(getCustomerNme());
		sb.append(DbUtil.DELIMITER);
		sb.append(getContactNo());
		sb.append(DbUtil.DELIMITER);
		sb.append(getInterest());
		return sb.toString();
	}
}
